package com.monodaipoly2.view;


import com.google.appengine.api.datastore.Key;
import com.monodaipoly2.persistencia.Rol;
import com.monodaipoly2.servicio.RolServicio;
import java.text.SimpleDateFormat;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;
import org.springframework.web.jsf.FacesContextUtils;

@Component
public class RolConverter implements javax.faces.convert.Converter{

    private RolServicio getRolServicio(FacesContext context){
        return (RolServicio)FacesContextUtils.getWebApplicationContext(context).getBean("rolServicio");
    }

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {

        String rdo = null;
        if (o instanceof Key) {
            Key keyRol = (Key) o;
            Rol rol = getRolServicio(FacesContext.getCurrentInstance()).getRol(keyRol);
            if (rol.getNombre() != null) {
                rdo = rol.getNombre();
            }

        }
        return rdo;

    }

}
